package com.wxtt.android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.fast.dev.frame.RealmUtils;
import com.fast.dev.frame.ui.Bind;
import com.wxtt.android.R;
import com.wxtt.android.adapter.ArticleAdapter;
import com.wxtt.android.bean.WxArticleBean;
import com.wxtt.android.common.CommonActivity;
import com.wxtt.android.config.ConstantConfig;
import com.wxtt.android.dialog.TipDialog;
import com.wxtt.android.realm.table.FavouriteRealm;
import com.wxtt.android.view.SwipeRefresh;

import java.util.ArrayList;
import java.util.List;

import io.realm.RealmResults;


/**
 * 说明：我的收藏
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2016/2/17 15:09
 * <p/>
 * 版本：verson 1.0
 */
public class FavouriteActivity extends CommonActivity implements SwipeRefresh.RefreshListener, AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener, TipDialog.TipClickListener {

    //下拉刷新
    @Bind(id = R.id.swipe_refresh)
    protected SwipeRefresh swipe_refresh;
    @Bind(id = R.id.lv)
    protected ListView lv;

    //设置Adapter
    private ArticleAdapter mAdapter;
    private List<WxArticleBean> datas = new ArrayList<>();
    private RealmUtils mRealmUtils;
    private int position = -1;

    @Override
    public int setRootView() {
        return R.layout.activity_favourite;
    }

    @Override
    public void onInit(Bundle bundle) {
        super.onInit(bundle);
        //初始化下拉刷新组件
        initSwipeRefresh();
        mRealmUtils = RealmUtils.get(this, ConstantConfig.REALM_NAME.FAVOURITE);
        mAdapter = new ArticleAdapter(lv,datas,R.layout.item_wx_article);
        lv.setOnItemClickListener(this);
        lv.setOnItemLongClickListener(this);
        lv.setEmptyView(bind(R.id.empty));
        lv.setAdapter(mAdapter);
    }

    /**
     * 说明：获取所有的收藏文章数据
     * @return
     */
    private List<FavouriteRealm> getFavouriteDatas(){
        RealmResults<FavouriteRealm> list = mRealmUtils.findAll(FavouriteRealm.class);
        list.sort("date", false);
        return list;
    }

    /**
     * 说明：初始化下拉刷新组件
     */
    private void initSwipeRefresh() {
        swipe_refresh.setRefreshListener(this);
    }

    @Override
    public void onRefresh() {
        List<FavouriteRealm> newDatas = getFavouriteDatas();
        if (datas.size() != newDatas.size()){
            datas.clear();
            datas.addAll(convert(newDatas));
            mAdapter.refresh(datas);
        }
        swipe_refresh.setRefreshLoadedState();
    }

    private List<WxArticleBean> convert(List<FavouriteRealm> realms){
        List<WxArticleBean> list = new ArrayList<>();
        if (realms != null && !realms.isEmpty()){
            for (int i = 0;i < realms.size();i++){
                WxArticleBean bean = new WxArticleBean();
                FavouriteRealm realm = realms.get(i);
                bean.setId(realm.getId());
                bean.setUrl(realm.getUrl());
                bean.setMark(realm.getMark());
                bean.setFirstImg(realm.getFirstImg());
                bean.setSource(realm.getSource());
                bean.setTitle(realm.getTitle());
                list.add(bean);
            }
        }
        return list;
    }

    @Override
    protected void onResume() {
        super.onResume();
        swipe_refresh.autoRefresh();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Bundle bundle = new Bundle();
        bundle.putParcelable("detailBundle", datas.get(position));
        Intent intent = new Intent(this, AriticleDetailActivity.class);
        intent.putExtra("detailBundle", bundle);
        startActivity(intent);
    }

    @Override
    protected boolean showToolbar() {
        return true;
    }

    @Override
    protected void onCustomToolbar(View view, Toolbar toolbar) {
        super.onCustomToolbar(view, toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public int setToolbarTitle() {
        return R.string.navigation_item_collect;
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        this.position = position;
        showTipDialog(getString(R.string.str_remove_favourite),this);
        return true;
    }

    @Override
    public boolean onSure() {
        if (position >= 0 && position < datas.size()){
            mRealmUtils.delete(FavouriteRealm.class, "id", datas.get(position).getId());
            datas.remove(position);
            mAdapter.refresh(datas);
            shortToast("取消收藏");
        }
        return false;
    }

    @Override
    public boolean onCancel() {
        return false;
    }
}
